//
//  SelectedBanJIVC.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/24.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "SelectedBanJIVC.h"
#import "ShaiXuanCell.h"
static NSString *ident = @"ShaiXuanCell";
@interface SelectedBanJIVC ()
<
UICollectionViewDelegate
,UICollectionViewDataSource
>

@property(nonatomic,strong)UICollectionView *collectionView;




@property (nonatomic,strong)UIButton *okButton;

@property (nonatomic,strong)UIButton *cancelButton;

@end

@implementation SelectedBanJIVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.allArr = @[@"11111",@"222222",@"333333",@"4444444",@"55555"].mutableCopy;
    
    if (_allArr.count <= 0) {
        [self getData];
    }
    
//    UIButton *vtn =nil;
    
    
//    [vtn setAttributedTitle:<#(nullable NSAttributedString *)#> forState:<#(UIControlState)#>] ;
    
    

    [self.view addSubview:self.collectionView];
    
    
    self.collectionView.sd_layout
    .topSpaceToView(self.view, 74)
    .rightSpaceToView(self.view, 20)
    .leftSpaceToView(self.view, 20)
    .bottomSpaceToView(self.view, 80+70);
    
    [self.view addSubview:self.okButton];
    
    
    self.okButton.sd_layout
    .topSpaceToView(self.collectionView,0)
    .leftSpaceToView(self.view, 20)
    .heightIs(60)
    .widthIs((ScreenWidth-40)/2);
    
    [self.view addSubview:self.cancelButton];
    
    self.cancelButton.sd_layout
    .topSpaceToView(self.collectionView,0)
    .rightSpaceToView(self.view, 20)
    .heightIs(60)
    .widthIs((ScreenWidth-40)/2);
    
}



-(UIButton *)okButton{
    if (!_okButton) {
        _okButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [_okButton setTitle:@"确认" forState:(UIControlStateNormal)];
        
        [_okButton setBackgroundColor:ZhuTiColor];
        
        BTN_MSG_FUNC(_okButton, oKsender);
        
        [_okButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        
    }
    return _okButton;
}

-(UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
        
        [_cancelButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        
        [_cancelButton setBackgroundColor:[UIColor whiteColor]];
        
         BTN_MSG_FUNC(_cancelButton, cancelSender);
    }
    return _cancelButton;
}

-(void)oKsender{
    
    NSLog(@"确认");
    
    if (self.selBlock) {
        self.selBlock(_rightLabelArray);
    }
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)cancelSender{
     NSLog(@"取消");
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark ----------------------- 获取数据 -----------------------
-(void)getData {
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    
    NSString *lastUrl  = @"g=Client&m=Class&a=myClass"; // g=Client&m=Course&a=xuekeList //g=Client&m=Teacher&a=getGradeLis
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,lastUrl] parameter:parameter sucess:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        NNSLog(@"111 = %@",dic);
       self.allArr = dic[@"data"][@"class_list"];
//
        NNSLog(@"22 = %@", self.allArr);
        
//        NSDictionary *dics = dic[@"data"];

//        NNSLog(@"%@",dics[@"0"]);
        
        
//         = arr;
        
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * layout =     [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,self.view.frame.size.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        
        _collectionView.dataSource = self;
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerNib:[UINib nibWithNibName:ident bundle:nil] forCellWithReuseIdentifier:ident];
        
    }
    return _collectionView;
}

-(NSMutableArray *)rightLabelArray{
    if (!_rightLabelArray) {
        _rightLabelArray=[NSMutableArray array];
    }
    return _rightLabelArray;
}

-(NSMutableArray *)allArr{
    if (!_allArr) {
        _allArr = [NSMutableArray array];
    }
    return _allArr;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _allArr.count;
}

#pragma mark ----------------------- cell 点击------------------------

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ShaiXuanCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
    
//    NSString *str =[NSString stringWithFormat:@"%zd",indexPath.row];

//     NNSLog(@"%@",self.allArr[@"0"]);

    NNSLog(@"%@",self.allArr);

    
    cell.nameLabel.text= self.allArr[indexPath.row][@"name"];

    NSString *str = self.allArr[indexPath.row];

    if ([ self.rightLabelArray containsObject: str ])
    {
        cell.nameLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = ZhuTiColor;
    }
    else
    {
        cell.nameLabel.textColor = [UIColor blackColor];
        cell.backgroundColor = hui1Color;
    }
    return cell;
}


//UICollectionView被选中时调用的方法
- ( void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (self.rightLabelArray.count > 0) {
        if ([self.rightLabelArray containsObject:self.allArr[indexPath.row]]) {
            
            
            [_rightLabelArray removeObject:_allArr[indexPath.row]];
            
        }else{
            
            [_rightLabelArray addObject:_allArr[indexPath.row]];
        }
    }else{
       [_rightLabelArray addObject:_allArr[indexPath.row]];
    }

    
    [collectionView reloadData];
}

#define JianGe 10
#define GeShu 4

//定义每个UICollectionView 的大小
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    return CGSizeMake((ScreenWidth - JianGe*(GeShu+1-40)) / GeShu, 30);
}


//定义每个UICollectionView 的边距
- ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section
{
    return UIEdgeInsetsMake ( JianGe , JianGe , JianGe , JianGe );
    
}

//设置水平间距 (同一行的cell的左右间距）
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return JianGe;
}

//垂直间距 (同一列cell上下间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return JianGe;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
